<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单','error','/order.list/');
	}
	$ispay=array('0'=>'未支付','1'=>'已支付');
	$item=$this->model('order')->get_order($id);
	$item['status_title']=$this->order_status[$item['status']];
	$item['step0']=1;
	$item['step1']=$item['ispay'];
	if(($item['status']==1 && $item['sendtype']>1) || ($item['status']>=2 && $item['status']!=4 && $item['status']!=10)){
		$item['step2']=1;
	}
	if($item['status']==3 || $item['status']==3 || $item['status']==6|| $item['status']==16){
		$item['step3']=1;
	}
	if($isonlyverifygoods){
		$item['step3title']='核销时间_Verify time';
	}else if($item['isverify']==1){
		$item['step3title']='确认使用_Confirm to use';
	}
	else if($item['addressid'] || $item['address']){
		$item['step3title']='商家发货_Merchant shipping';
	}
	else if($item['isvirtualsend'] && $item['virtual']){
		$item['step3title']='自动发货_Automatic delivery';
	}else{
		$item['step3title']='确认取货_Confirm the pickup';
	}
	if($item['isverify']==1){
		$item['sendtypetitle']='线下核销_Offline cancel after verification';
	}else if($item['sendtype']==1 && ($item['addressid'] || $item['address'])){
		$item['sendtypetitle']='快递_Express';
		if($item['dispatchid']){
			$item['sendtypetitle']=pdo_getcolumn('jy_weishop_dispatch',array('id'=>$item['dispatchid'],'uniacid'=>$_SESSION['uniacid']),'title');
		}
	}else if($item['isvirtualsend'] && $item['virtual']){
		$item['sendtypetitle']='自动发货(虚拟物品)_Automatic delivery';
	}else if($item['isverify']==1){
		$item['sendtypetitle']='线下核销_Offline cancel after verification';
	}else if($item['sendtype']==2){
		$item['sendtypetitle']='自提_Since the lift';
	}else{
		$item['sendtypetitle']='其他_Other';
	}
	if($item['status']==0 && $item['paytype']==3){
		$item['statustitle']='待发货';
	}elseif($item['status']==0 && $item['paytype']!=3){
		$item['statustitle']='待付款';
	}elseif($item['status']==1 && $item['isverify']==1){
		$item['statustitle']='待使用';
	}elseif($item['status']==1 && $item['sendtype']==2){
		$item['statustitle']='待取货';
	}elseif($item['status']==1 && $item['sendtype']!=2 && $item['isverify']!=1 && $item['addressid']){
		if($item['sendtype']==3){
			$item['statustitle']='虚拟发货';
		}elseif($item['send_status']==0){
			$item['statustitle']='待发货';
		}else{
			$item['statustitle']=$this->send_status[$item['send_status']];
		}	
	}elseif($item['status']==2){
		$item['statustitle']='待收货';
	}elseif($item['status']==3){
		$item['statustitle']='交易完成';
	}elseif($item['status']==-1 || $item['status']==4){
		$item['statustitle']='已关闭';
	}else{
		$item['statustitle']=$this->order_status[$item['status']];
	}
	$pay_types=get_pay_types();
	$item['paytype2']=$item['paytype'];
	$item['paytype']=$pay_types[$item['paytype']];
	$item['address']=json_decode($item['address'],true);
	$item['api_data']=json_decode($item['api_data'],true);
	//$item['address']=model('core/member')->get_address_str($item['addressid']);
	if($item['address'] && ($item['address']['address'] || $item['address']['mobile'])){
		$address=$item['address'];
		$addressid=$item['address'];
	}else{
		$address=pdo_get('core_address',array('id'=>$item['addressid']));
		$addressid=$item['addressid'];
	}
	$item['address']=array(
		'address'=>model('core/member')->get_address_str($addressid),
		'address_en'=>model('core/member')->get_address_str($addressid,true),
		'mobile'=>$address['mobile'],
		'name'=>$address['name'],
		'area_code'=>$address['area_code'],
		'diyfields'=>json_decode($address['diyfields'],true)
	);
	$item['address']['address']=$item['address']['address'].'_'.$item['address']['address_en'];
	if($item['mid']){
		$item['member']=cfc('core')->member_show($item['mid']);
	}else{
		$item['member']=array('nickname'=>$address['name']);
	}
	$htmls=array();
	if($item['status']==1){
		//待发货
		$htmls[]=array(
			'type'=>'button_list',
			'list'=>array(
				array('type'=>'modal','url'=>'/order.list/fa/id='.$item['id'],'title'=>'发货'),
			),
		);
	}
	$htmls[]=array(
		'type'=>'look',
		'desc'=>'基本信息',		
		'look'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member.nickname','title'=>'买家昵称','type'=>'string'),
			array('name'=>'member.avatar','title'=>'买家头像','type'=>'image'),
			array('name'=>'member.mobile','title'=>'买家电话','type'=>'string'),
			array('name'=>'price','title'=>'订单金额','type'=>'string'),
			array('name'=>'goodsprice','title'=>'商品金额','type'=>'string'),
			array('name'=>'discountprice','title'=>'运费','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'status_title','title'=>'订单状态','type'=>'string'),
		)
	);
	//商品信息
	$htmls[]=array(
		'type'=>'look',
		'desc'=>'商品清单',		
		'look'=>array(
			array('name'=>'goods','title'=>'商品信息','type'=>'array','list'=>array(
				array('type'=>'string','name'=>'title','title'=>'商品名称'),
				array('type'=>'image','name'=>'image','title'=>'商品图片'),
				array('type'=>'string','name'=>'price','title'=>'购买单价'),
				array('type'=>'string','name'=>'total','title'=>'购买数量'),
			)),
		)
	);
	//支付
	$pay_show=array(
		'type'=>'look',
		'desc'=>'支付',
		'look'=>array(
			array('name'=>'ispay','title'=>'是否支付','type'=>'string'),
		),
	);
	if($item['ispay']){
		$pay_show['look'][]=array('name'=>'paytype','title'=>'支付方式','type'=>'string');
		$pay_show['look'][]=array('name'=>'paytime','title'=>'支付时间','type'=>'time');
	}
	$item['ispay']=$ispay[$item['ispay']];
	$htmls[]=$pay_show;
	//收货信息
	$htmls[]=array(
		'type'=>'look',
		'desc'=>'收货信息',
		'look'=>array(
			array('name'=>'address.address','title'=>'收货地址','type'=>'string'),
			array('name'=>'address.name','title'=>'收货人姓名','type'=>'string'),
			array('name'=>'address.mobile','title'=>'收货人电话','type'=>'string'),
		),
	);
	$express=$this->get('order_express',array('orderid'=>$item['id']));
	if($express){
		//发货信息
		$express['express_id']=$this->model('model')->express_types()[$express['express_id']]['name'];		
		$item['express']=$express;
		$htmls[]=array(
			'type'=>'look',
			'desc'=>'发货货信息',
			'look'=>array(
				array('name'=>'express.createtime','title'=>'发货时间','type'=>'time'),
				array('name'=>'express.express_id','title'=>'快递公司','type'=>'string'),
				array('name'=>'express.expresssn','title'=>'快递单号','type'=>'string'),
			),
		);
	}
	//$return['htmls']=$htmls;
	$order_verify=$this->pdo_getall('order_verify',array('orderid'=>$item['id']));
	if($order_verify){
			foreach($order_verify as &$verify){
				$verify['verifycode']=$verify['verify_code'];
				if($verify['status']==0 && $verify['endtime'] && $verify['endtime']<TIMESTAMP){
					$verify['status']=2;
					$this->pdo_update('order_verify',array('status'=>2),array('id'=>$verify['id']));
				}else{
					$verify['url']=custom_url('jy_weishop/verify.detail//id='.$item['id'].'&verifycode='.$verify['verify_code'],true);
					
				}
			}
	}
	$return['order_verify']=$order_verify;
	$goods_verify=$this->pdo_getall('verifygoods',array('orderid'=>$item['id']));
	if($goods_verify){
			foreach($goods_verify as &$gvv){
				if (!(empty($gvv['limitnum']))){
					$verifygoodlogs = pdo_getall('jy_weishop_verifygoods_log',array('verifygoodsid'=>$gvv['id']));
					$verifynum = 0;
					foreach ($verifygoodlogs as $verifygoodlog ) 
					{
						$verifynum += intval($verifygoodlog['verifynum']);
					}
					$gvv['lastverifys'] = intval($gvv['limitnum']) - $verifynum;
					if (empty($gvv['limittype'])) {
						$limitdate = intval($gvv['starttime']) + (intval($gvv['limitdays']) * 86400);
					}else{
						$limitdate = intval($gvv['limitdate']);
					}
					if ($limitdate < time()) {
						$gvv['status']=3;
					}
				}
				$gvv['url']=custom_url('jy_weishop/verify.verifygoodsdetail//id='.$item['id'].'&verifycode='.$gvv['verifycode'],true);
				
			}
	}
	$return['goods_verify']=$goods_verify;
	$item['diyformdata']=json_decode($item['diyformdata'],true);
	foreach($item['diyformdata'] as &$dv){
			$dv['name']='data';
			if($dv['type']==5){
				$dv['data']=str_array($dv['data']);
			}
	}
	if($item['sendtype']==1 || $item['sendtype']==4){
		if($item['peisong_id']){
			if(!$this->plugin_exists('jy_peisong')){
				exi('请联系管理员开通《配送员》插件！','error');
			}
			$return['peisong']=pdo_get('jy_peisong_member',array('mid'=>$item['peisong_id']));
		}else{
			$return['peisong']=pdo_get('jy_weishop_order_express',array('orderid'=>$item['id']));
		}
	}
	$order_gifts=$this->pdo_get('order_gift',array('order_id'=>$item['id']));
	if($order_gifts){
		$order_gifts=json_decode($order_gifts['gifts'],true);
		if($_SESSION['is_tomedia']){
		foreach($order_gifts as &$giv){
			$giv['image']=$giv['image']?tomedia($giv['image']):'';
		}
		}
	}
	$return['order_gifts']=$order_gifts;
	$item['invoice']=$this->pdo_get('order_invoice',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']));
	$item['paylog']=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pay_id']));
	if($item['paylog']['di_fee']>0){
		$credit_types=credit_types(1,1);
		$item['paylog']['difeetitle']=$credit_types[$item['paylog']['di_credit']].'抵扣';
	}
	$item['payfee']=$item['price']-$item['paylog']['di_fee'];
	$item['paydiyfields']=$item['paylog']['diyfields'];
	$item['paydiyfields']=json_decode($item['paydiyfields']);
	$return['self']=pdo_get('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']));
	//蚂蚁数据
	$member=pdo_get('core_members',array('id'=>$item['mid']),array('avatar','username','nickname','isrealname','realname','mobile','realmobile','cardnum','cardinfo','sex','email','birth'));
	if($member['sex']==1){
		$member['sex']='男';
	}elseif($member['sex']==2){
		$member['sex']='女';
	}elseif($member['cardnum']){
		$num=substr($member['cardnum'],16,1);
		if($num%2==0){
			$member['sex']='女';
		}else{
			$member['sex']='男';
		}
	}else{
		$member['sex']='未知';
	}
	$member['birth']=json_decode($member['birth'],true);
	if($member['birth']['year']){
		$newyear=date('Y');
		$member['age']=$newyear-$member['birth']['year'];
	}elseif($member['cardnum']){
		$year=substr($member['cardnum'],6,4);
		$newyear=date('Y');
		$member['age']=$newyear-$year;
	}
	$levelid=pdo_getcolumn('jy_weishop_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'level');
	$member['leveltitle']=pdo_getcolumn('jy_weishop_member_level',array('id'=>$levelid,'uniacid'=>$_SESSION['uniacid']),'title');
	$member['leveltitle']=$member['leveltitle']?:'普通会员';
	$return['member']=$member;
	if($item['merchid']>0 && $this->plugin_exists('jy_merch')){
		$item['merch']=pdo_get('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),array('id','merchname','realname','mobile','address'));
	}
	$return['peerpay']=pdo_get('jy_weishop_order_peerpay_log',array('status'=>1,'orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
	$return['peerpay']['nickname']=pdo_getcolumn('core_members',array('id'=>$return['peerpay']['mid'],'uniacid'=>$_SESSION['uniacid']),'nickname');
	if($return['peerpay']['id']){
		$item['paytype']='找人代付(ID:'.$return['peerpay']['mid'].')';
	}
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='receive'){
	$post=get_postdata();
	$id=intval($post['id']);
	exi('暂时不支持，后台手动收货！','error');
	$this->model('order')->order_receive($id);
	exi('收货成功！');	
}
if($_RQ['op']=='cancelfh'){
	$post=get_postdata();
	$id=intval($post['id']);
	$this->model('order')->order_cancelfh($id);
	exi('取消发货成功！','','/order.list/');	
}
if($_RQ['op']=='upaddress'){
	$id=intval($_RQ['id']);
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->update('order',array('address'=>json_encode($post['address'])),array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('修改收货信息成功！','','/order.list/');
	}
	$item=$this->get('order',array('id'=>$id),array('id','address','realname','mobile'));
	$item['address']=json_decode($item['address'],true);
	if($item['address'] && $item['address']['name']){	
		$address=$item['address'];
	}else{
		$address=pdo_get('core_address',array('id'=>$item['addressid']));
	}
	if(!is_array($address['pcd'])){
		$address['pcd']=json_decode($address['pcd'],true);
	}
	$item['address']=$address;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改收货信息',
	);
	$htmls['edit']=array(
			array('name'=>'address.name','title'=>'收货人姓名','type'=>'string'),
			array('name'=>'address.mobile','title'=>'收货人电话','type'=>'string'),
			array('name'=>'address.pcd','title'=>'收货地址','type'=>'pcd'),
			array('name'=>'address.address','title'=>'收货地址','type'=>'string'),
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='remark'){
	$id=intval($_RQ['id']);
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->update('order',array('remark'=>$post['remark']),array('id'=>$post['id']));
		exi('备注成功！','','/order.list/');
	}
	$item=$this->get('order',array('id'=>$id),array('id','remark'));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改备注',
	);
	$htmls['edit']=array(
		array('type'=>'text','title'=>'备注内容','name'=>'remark','help'=>'填写备注内容！'),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='upprice'){
	$id=intval($_RQ['id']);
	if($_RQ['postdata']){
		$post=get_postdata();	
		if($post['price']){
			if(!is_numeric($post['price'])){
				exi('价格必须是数字！','error');
			}
			if(!method_exists(cfc('pay'),'change_pay_fee')){
				exi('修改价格失败！请联系管理员，更新框架到最新','error');
			}
			$price=floatval($post['price']);
			$orderprice=floatval($post['orderprice']);
			$dispatchprice=floatval($post['dispatchprice']);
			if ($price < 0){
				$price = 0;
			}
			$this->model('order')->upprice($post['id'],$price,$orderprice,$dispatchprice);
			exi('改价成功！','','/order.detail//id='.$post['id']);	
		}
	}
	$item=$this->get('order',array('id'=>$id),array('id','price','dispatchprice','sendtype'));
	$item['orderprice']=$item['price']-$item['dispatchprice'];
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改价格',
	);
	$htmls['edit']=array(
		array('type'=>'copy','title'=>'订单原价','name'=>'price'),
		array('type'=>'string','title'=>'修改价格','name'=>'orderprice','help'=>'修改价格！'),
		array('type'=>'string','title'=>'修改运费','name'=>'dispatchprice','help'=>'修改运费！','show'=>array('sendtype'=>1)),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='lookexpress'){
	$id=intval($_RQ['id']);
	$item=$this->get('order_express',array('orderid'=>$id));
	$express_types=$this->model('model')->express_types();
	$item['express_name']=$express_types[$item['express_id']]['name'];
	$item['express']=$express_types[$item['express_id']]['express'];
	$return['expresslist']=$this->model('model')->getExpressList($item['express'],$item['expresssn'],'',$item['orderid']);
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='export'){
	$id=intval($_RQ['id']);
	$item=$this->model('order')->get_order($id);
	$item['status_title']=$this->order_status[$item['status']];
	if($isonlyverifygoods){
		$item['step3title']='核销时间';
	}else if($item['isverify']==1){
		$item['step3title']='确认使用';
	}
	else if($item['addressid']){
		$item['step3title']='商家发货';
	}
	else if($item['isvirtualsend'] && $item['virtual']){
		$item['step3title']='自动发货';
	}else{
		$item['step3title']='确认取货';
	}
	if($item['isverify']==1){
		$item['sendtypetitle']='线下核销';
	}else if($item['addressid']){
		$item['sendtypetitle']='快递';
	}else if($item['isvirtualsend'] && $item['virtual']){
		$item['sendtypetitle']='自动发货(虚拟物品)';
	}else if($item['isverify']==1){
		$item['sendtypetitle']='线下核销';
	}else if($item['dispatchtype']){
		$item['sendtypetitle']='自提';
	}else{
		$item['sendtypetitle']='其他';
	}
	$paytype=get_pay_types();
	$ispay=array('0'=>'未支付','1'=>'已支付');
	$item['paytype2']=$item['paytype'];
	$item['paytype']=$paytype[$item['paytype']];
	$item['ispay']=$ispay[$item['ispay']];
	$member=model('core/member')->detail($item['mid'],array('nickname','avatar','mobile'));
	$item=array_merge($member,$item);
	//$item['address']=model('core/member')->get_address_str($item['addressid']);
	//用户地址
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && ($order['address']['name'] || $order['address']['mobile'])){
		$address=$order['address'];
	}else{
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
	}
	$address=array(
		'address'=>model('core/member')->get_address_str($address),
		'address_en'=>model('core/member')->get_address_str($address,true),
		'mobile'=>$address['mobile'],
		'name'=>$address['name'],
		'area_code'=>$address['area_code'],
	);
	$item=array_merge($item,$address);
	$item['createtime']=$item['createtime']?date('Y-m-d H:i:s',$item['createtime']):'';
	$item['payime']=$item['payime']?date('Y-m-d H:i:s',$item['payime']):'';
	$item['sendtime']=$item['sendtime']?date('Y-m-d H:i:s',$item['sendtime']):'';
	$item['finishtime']=$item['finishtime']?date('Y-m-d H:i:s',$item['finishtime']):'';
	$item['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$item['storeid']),'title')?:'无';
	$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['id']),array('goodsid','price','total'));
	$item['orderprice']=$item['price'];
	foreach($goods as $k=>$gv){
		$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid']),array('title','goodssn','productsn','offcode','unit'));
		$goodsinfo['title']=str_replace(',',' ',$goodsinfo['title']);
		//商品规格
		if($gv['optionid']){
				$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid'],'uniacid'=>$_SESSION['uniacid']),array('title','specs','goodssn','productsn'));
				$specs=$options['specs'];
				if($gv['istaocan']){
					$gv['selespename']=$options['title'];
				}else{
					$specs=explode("_",$specs);
					foreach($specs as &$spec){
						$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec,'uniacid'=>$_SESSION['uniacid']),'title');
					}
					$gv['selespename']=implode("，",$specs);
				}
		}
		$newv['title']=$goodsinfo['title'];
		if($gv['selespename']){
			$newv['title'] .='['.$gv['selespename'].']';
		}
		$newv['goodssn']=$goodsinfo['goodssn']?:'无';
		$newv['productsn']=$goodsinfo['productsn']?:'无';
		$newv['offcode']=$goodsinfo['offcode']?:'无';
		$newv['unit']=$goodsinfo['unit']?:'件';
		$newv['price']=$gv['price'];
		$newv['total']=$gv['total'];
		$newv=array_merge($item,$newv);
		$exdata[]=$newv;
	}
	$header=array(
		'ordersn'=>'订单号',
		'storetitle'=>'所属门店',
		'title'=>'商品名称',
		'goodssn'=>'商品编码',
		'productsn'=>'商品条码',
		'unit'=>'单位',
		'total'=>'数量',
		'price'=>'单价',
		'orderprice'=>'总金额',
		'status_title'=>'订单状态',
		'ispay'=>'支付状态',
		'paytype'=>'支付方式',
		'nickname'=>'买家昵称',
		'name'=>'收货人姓名',
		'address'=>'收货地址',
		'createtime'=>'下单时间',
		'payime'=>'支付时间',
		'sendtime'=>'发货时间',
		'finishtime'=>'完成时间',
	);
	cfc('tools')->export2excel($header,$exdata,'订单详情');
}